package medium;
/*
19. 删除链表的倒数第 N 个结点
 */
public class LeeCode19 {

  public static void main(String[] args) {

  }

  public ListNode removeNthFromEnd(ListNode head, int n) {
    int length = getLength(head);
    ListNode prev = new ListNode(0,head);
    ListNode cur = prev;
    for (int i = 0; i < length - n; i++) {
      cur = cur.next;
    }
    cur.next = cur.next.next;
    return prev.next;
  }

  public int getLength(ListNode head) {
    int length = 0;
    while (head != null) {
      length++;
      head = head.next;
    }
    return length;
  }

  class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }

}
